#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include "contactinfo.h"
#include "contactlist.h"
#include "sqlutil.h"
#include "talkmsglist.h"
#include <QMainWindow>
#include <QMediaPlayer>
#include <QUdpSocket>
#include <QSound>
#define GET_MODE(command) (command & 0x000000ffUL)
//命令
#define IPMSG_BR_ENTRY 0x00000001UL  //上线
#define IPMSG_BR_EXIT  0x00000002UL  //下线
#define IPMSG_ANSENTRY 0x00000003UL  //响应新上线通知
#define IPMSG_BR_ABSENCE  0x00000004UL //更改为离开状态
#define IPMSG_BR_ISGETLIST  0x00000010UL  //搜寻有效的主机用户
#define IPMSG_OKGETLIST     0x00000011UL  //主机列表发送通知
#define IPMSG_GETLIST            0x00000012UL   //主机列表发送请求
#define IPMSG_ANSLIST            0x00000013UL   //主机列表发送
#define IPMSG_BR_ISGETLIST2        0x00000018UL
#define IPMSG_SENDMSG 0x00000020UL  //发送消息
#define IPMSG_RECVMSG            0x00000021UL  //接收消息确认
#define IPMSG_READMSG            0x00000030UL  //消息打开通知
#define IPMSG_DELMSG            0x00000031UL //消息丢弃通知
#define IPMSG_ANSREADMSG        0x00000032UL  //消息打开确认通知
#define IPMSG_GETINFO            0x00000040UL   //飞鸽版本信息
#define IPMSG_SENDINFO            0x00000041UL  //传送飞鸽版本信息
#define IPMSG_GETABSENCEINFO    0x00000050UL //获取离线判定
#define IPMSG_SENDABSENCEINFO    0x00000051UL  //发送离线判定
#define IPMSG_GETFILEDATA        0x00000060UL //基于TCP的⽂件传送请求
#define IPMSG_RELEASEFILES        0x00000061UL //丢弃（取消）附件⽂件的接收
#define IPMSG_GETDIRFILES        0x00000062UL //⽂件夹传书请求
#define IPMSG_GETPUBKEY            0x00000072UL //公钥获取
#define IPMSG_ANSPUBKEY            0x00000073UL  //公钥响应


QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    QString getHostIpAddress(QString hostName);
    QString getHostMacAddress();
    const int sourcePort=2425;//要绑定的本机端口号
    const int destPort=2425;  //目标端口号
    QString version="xfg100";  //软件版本号
    QString package="100"; //包号
    QString sourceIp; //本机ip地址
    QString sourceMac; //本机mac地址
    QString sourceHostUserName;//本机主机用户名
    QString sourceHostName; //本机主机名
    SqlUtil *sqlUtil=nullptr;  //数据库
    ContactList *contactList; //好友列表组件
    TalkMsgList *talkMsgList; //消息列表组件
    ContactInfo *curContact=nullptr;  //当前打开的会话联系人
    enum MsgProtocol{
        Xfg
    };

    enum MsgTypeCode {
        Online = GET_MODE(IPMSG_BR_ENTRY),
        AnsOnline = GET_MODE(IPMSG_ANSENTRY),
        Offline = GET_MODE(IPMSG_BR_EXIT),
        SendMsg = GET_MODE(IPMSG_SENDMSG),
        GetPubKey=GET_MODE(IPMSG_GETPUBKEY),
        AnsPubKey=(IPMSG_ANSPUBKEY),
    };
    void closeEvent(QCloseEvent *);
private slots:
    void on_sendMsgBtn_clicked();
    void playNewMsgSound();

    void on_captureScreenBtn_clicked();

private:
    Ui::MainWindow *ui;
    QUdpSocket *mSocket;
    void reportOnline();
    void sendHandle(QString readyMsg,QHostAddress addr);
    void receiveHandle();
    QString buildFormatMsg(MsgProtocol msgProtocol, MsgTypeCode msgTypeCode,QString msg="",ContactInfo *contactInfo=nullptr);
    void addToContactList(ContactInfo *contactInfo);
    void addMsgHandle(ContactInfo *contactInfo);
};
#endif // MAINWINDOW_H
